<?php
	include('header.php');
	
	if(!isset($user)) header('Location: index.php');
	
	$empty_fields = array();
	
	if(isset($_POST['form_edit_pwd'])){
		if(empty($_POST['form_edit_pwd_b'])){
			$error = 0;
			$empty_fields[] = 'form_edit_pwd_b';
		}
		if(empty($_POST['form_edit_pwd_n'])){
			$error = 0;
			$empty_fields[] = 'form_edit_pwd_n';
		}
		if(empty($_POST['form_edit_pwd_nc'])){
			$error = 0;
			$empty_fields[] = 'form_edit_pwd_nc';
		}
		if(!isset($error) && $_POST['form_edit_pwd_n'] != $_POST['form_edit_pwd_nc']){
			$error = 1;
		}
		if(!isset($error) && !$user->hasPwd($_POST['form_edit_pwd_b'])){
			$error = 2;
		}
		if(!isset($error)){
			$user->changePwd($_POST['form_edit_pwd_n']);
		}
	}
	
	include(TPL_DIR . 'edit-password.html');
	
	include('footer.php');
?>